<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>规格管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
    
</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->                
                    <div class="box-header with-border">
                        <h3 class="box-title">规格管理</h3>
                    </div>
                    <div class="box-body">
                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick="deleteBatch()"><i class="fa fa-trash-o"></i> 删除</button>
                                        
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                    规格名称：<input  id="specName">
									<button class="btn btn-default" onclick="getSpecificationList()">查询</button>
                                </div>
                            </div>
                            <!--工具栏/-->
			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">规格ID</th>
									      <th class="sorting">规格名称</th>									     												
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="spectable"></tbody>
			                  </table>
			                  <!--数据列表/-->    
                        </div>
                        <!-- 数据表格 /--> 
                     </div>
                    <!-- /.box-body -->                    
	          		    
                                
<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">规格编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px" id="specOption">
		      	<tr>
		      		<td>规格名称</td>
		      		<td><input  class="form-control" placeholder="规格名称" name="specName" id="specName1">  </td>
		      	</tr>
			 </table>				
			 
			 <!-- 规格选项 -->
			 <div class="btn-group">
                  <button type="button" class="btn btn-default" title="新建" id="insertTr"><i class="fa fa-file-o"></i> 新增规格选项</button>
                  
             </div>
			 
			 <table class="table table-bordered table-striped table-hover dataTable" id="table">
                    <thead>
                        <tr>
				      <th class="sorting">规格选项</th>
				      <th class="sorting">排序</th>																
                      <th class="sorting">操作</th>
						</tr>
                    </thead>
                    <tbody>
                      <tr>

				            <td>
				            	<input  class="form-control" placeholder="规格选项">
				            </td>
				            <td>
				            	<input  class="form-control" placeholder="排序">
				            </td>
							<td>
								<button type="button" class="btnDelete" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>
							</td>
                      </tr>
					  <tr>

				            <td>
				            	<input  class="form-control" placeholder="规格选项">
				            </td>
				            <td>
				            	<input  class="form-control" placeholder="排序">
				            </td>
							<td>
								<button type="button" class="btnDelete" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>
							</td>
                      </tr>
					  <tr>

				            <td>
				            	<input  class="form-control" placeholder="规格选项">
				            </td>
				            <td>
				            	<input  class="form-control" placeholder="排序">
				            </td>
							<td><button type="button" class="btnDelete" title="删除" ><i class="fa fa-trash-o"></i> 删除</button></td>
                      </tr>
                    </tbody>
			  </table> 
			
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="insert()">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>
<!--修改-->
  <div class="modal fade" id="editModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabel1">规格编辑</h3>
			  </div>
			  <div class="modal-body">
				<form id="updateSpecTable">
				  <table class="table table-bordered table-striped"  width="800px" id="specOption1">
					  <input type="hidden" name="id" id="specId">
					  <tr>
						  <td>规格名称</td>
						  <td><input  class="form-control" placeholder="规格名称" name="specName" id="specName2">  </td>
					  </tr>
				  </table>

				  <!-- 规格选项 -->
				  <div class="btn-group">
					  <button type="button" class="btn btn-default" title="新建" id="insertTr1"><i class="fa fa-file-o"></i> 新增规格选项</button>

				  </div>

				  <table class="table table-bordered table-striped table-hover dataTable" id="table1">
					  <thead>
					  <tr>
						  <th class="sorting">规格选项</th>
						  <th class="sorting">排序</th>
						  <th class="sorting">操作</th>
					  </tr>
					  </thead>
					  <tbody id="updateTable">
<!--					  <tr>-->

<!--						  <td>-->
<!--							  <input  class="form-control" placeholder="规格选项">-->
<!--						  </td>-->
<!--						  <td>-->
<!--							  <input  class="form-control" placeholder="排序">-->
<!--						  </td>-->
<!--						  <td>-->
<!--							  <button type="button" class="btnDelete" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>-->
<!--						  </td>-->
<!--					  </tr>-->
<!--					  <tr>-->

<!--						  <td>-->
<!--							  <input  class="form-control" placeholder="规格选项">-->
<!--						  </td>-->
<!--						  <td>-->
<!--							  <input  class="form-control" placeholder="排序">-->
<!--						  </td>-->
<!--						  <td>-->
<!--							  <button type="button" class="btnDelete" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>-->
<!--						  </td>-->
<!--					  </tr>-->
<!--					  <tr>-->

<!--						  <td>-->
<!--							  <input  class="form-control" placeholder="规格选项">-->
<!--						  </td>-->
<!--						  <td>-->
<!--							  <input  class="form-control" placeholder="排序">-->
<!--						  </td>-->
<!--						  <td><button type="button" class="btnDelete" title="删除" ><i class="fa fa-trash-o"></i> 删除</button></td>-->
<!--					  </tr>-->
					  </tbody>
				  </table>
				</form>

			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updateInfo()">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>

</body>

</html>
<script>
	$(function(){
		getSpecificationList();
	})

	function getSpecificationList(){
		$.ajax({
			url:"/specificationController/specificationList",
			type:"post",
			dataType:"json",
			data:{specName:$("#specName").val()},
			success:function(result){
					var list = result.data;
					var table = "";
					for (var i=0;i < list.length;i++){
					table +="<tr><td><input  type='checkbox' value='"+list[i].id+"'></td>";
					table +="<td>"+list[i].id+"</td>";
					table +="<td>"+list[i].specName+"</td>";
					table +="<td class='text-center'>";
					table +="<button type='button' class='btn bg-olive btn-xs' data-toggle='modal' data-target='#editModal1' onclick='toUpdate("+ list[i].id +")'>修改</button>";
					table +="</td></tr>";
					}
					$("#spectable").html(table);
			},
			error:function (result){
				alert("错误");
			}
		})
	}

	//表格尾部增加一行
	$(function(){
		$("#insertTr").click(function(){
			$("#table tbody").append('<tr><td><input  class="form-control" placeholder="规格选项"></td><td><input  class="form-control" placeholder="排序"></td><td><button type="button" class="btnDelete" title="删除" ><i class="fa fa-trash-o"></i> 删除</button></td></tr>');
		});
	})

	//表格删除任意一行
	$(function(){
		$("#table").on('click', '.btnDelete', function () {
			$(this).closest('tr').remove();
		});
	})

	//表格尾部增加一行
	$(function(){
		$("#insertTr1").click(function(){
			$("#table1 tbody").append('<tr><td><input  class="form-control" placeholder="规格选项"></td><td><input  class="form-control" placeholder="排序"></td><td><button type="button" class="btnDelete" title="删除" ><i class="fa fa-trash-o"></i> 删除</button></td></tr>');
		});
	})

	//表格删除任意一行
	$(function(){
		$("#table1").on('click', '.btnDelete', function () {
			$(this).closest('tr').remove();
		});
	})

	function insert(){
		var optionList = [];
		var table = $("#table");
		var trs = $(table).find("tr");
		console.log(table);
		console.log(trs);
		for (var i = 1;i<trs.length;i++){
			var tds = $(trs[i]).find("td");
			var option = {"optionName":$(tds[0]).find("input").val(),"orders":$(tds[1]).find("input").val()}
			optionList.push(option);
		}
		var optionListJson = JSON.stringify(optionList)
		console.log(optionListJson)
		$.ajax({
			url: "/specificationController/SaveSpecification",
			type: "post",
			dataType: "json",
			data: {specName:$("#specName1").val(),optionListJson:optionListJson},
			success:function(result){
				alert("增加成功")
				window.location.reload();
			},
			error:function (result){
				alert("错误");
			}
		 })
	}

	function toUpdate(id){
		$.ajax({
			url:"/specificationController/toUpdate?id="+id,
			type:"post",
			dataType:"json",
			data:{},
			success:function(result){
				$("#specId").val(result.data.id);
				$("#specName2").val(result.data.specName)
				var table = "";
				var list = result.data.list;
				for (var i=0;i<list.length;i++){
					table += "<tr><td><input  class='form-control' placeholder='规格选项' value='"+list[i].optionName+"'></td>";
					table += "<td><input  class='form-control' placeholder='排序' value='"+list[i].orders+"'></td>";
					table +="<td><button type='button' class='btnDelete' title='删除' ><i class='fa fa-trash-o'></i> 删除</button></td></tr>";
				}
				$("#updateTable").html(table);
			},
			error:function(result){
				alert("错误");
			}
		})
	}

	function updateInfo(){
		var optionList = [];
		var table = $("#updateTable");
		var trs = $(table).find("tr");
		console.log(table);
		console.log(trs);
		for (var i = 0;i<trs.length;i++){
			var tds = $(trs[i]).find("td");
			var option = {"optionName":$(tds[0]).find("input").val(),"orders":$(tds[1]).find("input").val()}
			optionList.push(option);
		}
		var optionListJson = JSON.stringify(optionList)
		var id = $("#specId").val();
		var specName = $("#specName2").val();
		$.ajax({
			url:"/specificationController/updateInfo",
			type:"post",
			dataType:"json",
			data:{id:id,specName:specName,optionListJson:optionListJson},
			success:function (result){
				alert("修改成功");
				location.reload();
			},
			error:function(result){
				alert("错误");
			}
		})
	}

	function deleteBatch(){
		var ids = [];
		var boxs=$("[type=checkbox]:checked");
		$(boxs).each(function (i,e){
			ids.push(e.value);
		})
		if (ids.length <0 ){
			alert("请选择要删除的数据");
			return ;
		}
		$.ajax({
			url:"/specificationController/deleteBatch",
			type:"post",
			dataType:"json",
			data:{ids:ids},
			success:function (result){
				alert("删除成功");
				location.reload();
			},
			error:function (result){
				alert("错误")
			}
		})

	}
</script>